import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Demo1 {
    public static void main(String[] args) {
        BlockingQueue<Integer> blockingQueue=new LinkedBlockingQueue<>(1000);
        Thread producer=new Thread(()->{
           int n=0;
           while (true){
               try {
                   blockingQueue.put(n);
                   System.out.println("生产元素"+n);
                   n++;
                   //Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
        },"producer");
       Thread consumer=new Thread(()->{
          while (true){
              try {
                  Integer n = blockingQueue.take();
                  System.out.println("消费元素"+n);
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  throw new RuntimeException(e);
              }
          }
       },"consumer");
       producer.start();
       consumer.start();
    }
}